--
-- Author: shunguo.chen
-- Date: 2019-05-23 20:12:24
--
-- SkillView 技能view
--

local TabsBtn = require("app.widget.TabsBtn")
local SkillPanel = import(".SkillPanel")
local SkillTalentPanel = import(".SkillTalentPanel")
local SkillRunePanel = import(".SkillRunePanel")
local SpecTypeRender = import(".renders.SpecTypeRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.SkillScene

function ClassRef:init()
    self._ui.Button_close:addClickEventListener(function()
            executeFunc(self._onCloseView)
        end)

    -- TabsBtn
    local btns = {
        self._ui["Panel/Panel_tab/selected_btn_1"],
        self._ui["Panel/Panel_tab/selected_btn_2"],
        self._ui["Panel/Panel_tab/selected_btn_3"],
    }
    self._binds = {}
    local function onTabsBtn(index, btn)
        if self._curIdx then
            self._curIdx = index
            self:_switchPanel()
        else
            self._curIdx = index
        end
    end
    self._tabsBtn = TabsBtn.new(btns, onTabsBtn, nil, self._binds)

    --左侧specTypeRenders
    local specTypeRenders = {}
    for i=1,3 do
        local node = self._ui["Panel/Panel_class/class_" .. i]
        local render = SpecTypeRender.create(node)
        render:onBtnClick(handler(self, self._setCurRender))
        specTypeRenders[i] = render
    end
    self._specTypeRenders = specTypeRenders

    self._specTip = self._ui["Panel/Panel_class/tips"]
    self._specActiveBtn = self._ui["Panel/Panel_class/Button"]
    self._specActiveBtnText = self._ui["Panel/Panel_class/Button/name"]
    self._specActiveBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "specType_active", self._curRender:getData())
    end)

    self._ui.skill:setVisible(false)
    self._ui.talent:setVisible(false)
    self._ui.rune:setVisible(false)

    self:refreshRedPoint()
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
    local funcId
    if self._curIdx == 1 then
        funcId = Constant.FUNC_TYPE_SKILL
    elseif self._curIdx == 2 then
        funcId = Constant.FUNC_TYPE_SKILL_TALENT
    end

    if not funcId then return end

    local params = {
        scene = funcId,
        sceneSelf = self,
        getFunc = self.getGuideNodeFunc,
        nextFunc = self.checkGuide
    }
    EM:notify("checkGuide", params)
end
-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 107 or id == 125 or id == 126 or id == 127 or id == 502 then
        -- 技能面板引导
        return self._binds[1]:getGuideNode(id)
    elseif id == 108 then
        -- 天赋提示1，点击切换天赋按钮
        return self._tabsBtn:getBtn(2)
    elseif id == 109 or id == 110 or id == 111 then
        -- 天赋面板引导
        return self._binds[2]:getGuideNode(id)
    elseif id == 165 then
        -- 第二个专精按钮
        return self._specTypeRenders[2]:getChildByName("Panel")
    elseif id == 166 then
        -- 激活专精按钮
        return self._specActiveBtn
    end
end

function ClassRef:remove()
    for k,panel in pairs(self._binds) do
        if panel.remove then
            panel:remove()
        end
    end
end

function ClassRef:show(index)
    self.super.show(self)
    self:_switchPanel(index)
end

function ClassRef:onSwitchPanel(cb)
    self._onSwitchPanel = cb
end

function ClassRef:getPanel(index)
    return self._binds[index]
end

function ClassRef:refreshRedPoint(redPointKey)
    if not redPointKey then
        local skillNew = UD.redPoint:getStateByKey(RedPointConstant.Skill_Skill)
        local talentNew = UD.redPoint:getStateByKey(RedPointConstant.Skill_Talent)
        local runeNew = UD.redPoint:getStateByKey(RedPointConstant.Skill_Rune)
        self._tabsBtn:getBtn(1):getChildByName("dot"):setVisible(skillNew)
        self._tabsBtn:getBtn(2):getChildByName("dot"):setVisible(talentNew)
        self._tabsBtn:getBtn(3):getChildByName("dot"):setVisible(runeNew)
    else
        local isNew = UD.redPoint:getStateByKey(redPointKey)
        if redPointKey == RedPointConstant.Skill_Skill then
            self._tabsBtn:getBtn(1):getChildByName("dot"):setVisible(isNew)
        elseif redPointKey == RedPointConstant.Skill_Talent then
            self._tabsBtn:getBtn(2):getChildByName("dot"):setVisible(isNew)
        elseif redPointKey == RedPointConstant.Skill_Rune then
            self._tabsBtn:getBtn(3):getChildByName("dot"):setVisible(isNew)
        end
    end
end

function ClassRef:_switchPanel(index)
    if index then
        self._curIdx = index
        self._tabsBtn:setSelected(index)
    end
    self:_createPanel(self._curIdx)

    -- 点击过页签红点就去掉了
    if self._curIdx == 1 then
        UD.redPoint:clearStateByKey(RedPointConstant.Skill_Skill)
    elseif self._curIdx == 2 then
        UD.redPoint:clearStateByKey(RedPointConstant.Skill_Talent)
    elseif self._curIdx == 3 then
        UD.redPoint:clearStateByKey(RedPointConstant.Skill_Rune)
    end

    executeFunc(self._onSwitchPanel, self._curIdx)

    -- guide
    self:checkGuide()
end

function ClassRef:_setCurRender(render)
    if self._curRender then
        self._curRender:setSelected(false)
    end
    render:setSelected(true)
    self._curRender = render

    local specType = render:getData()
    executeFunc(self._onOperate, "specType_switch", specType)

    local isActive = specType:isActive()
    self._specTip:setVisible(isActive)
    self._specActiveBtn:setVisible(not isActive)
    if not isActive then
        local isOpen = specType:isOpen()
        local nameStr
        if isOpen then
            nameStr = L("lua_code_text_50")
        else
            nameStr = string.format(L("lua_code_text_378"), specType:getUnlockLevel())
        end
        self._specActiveBtnText:setString(nameStr)
        display.setBtnTouchEnable(self._specActiveBtn, isOpen)
    end
end

-- specTypes {SpecType, ...} 专精数据 
-- curSpecId 选择的specId
function ClassRef:refresh(specTypes, curSpecId)
    local curRender, specType
    for i,render in ipairs(self._specTypeRenders) do
        specType = specTypes[i]
        render:setData(specType)
        if curSpecId and specType:getId() == curSpecId then
            curRender = render
        end
    end

    self:_setCurRender(curRender or self._specTypeRenders[1])
end

function ClassRef:_createPanel(index)
    if not self._binds[index] then
        local panel
        if index == 1 then
            panel = SkillPanel.create(self._ui.skill)
            panel:setShowType(1)
        elseif index == 2 then
            panel = SkillTalentPanel.create(self._ui.talent)
        else
            panel = SkillRunePanel.create(self._ui.rune)
        end
        if panel.onOperate then
            panel:onOperate(self._onOperate)
        end
        panel:setVisible(true)
        self._binds[index] = panel
    end
end

return ClassRef
